package com.seeyon.apps.relocationData.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.relocationData.dao.RelocationDao;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumBean;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import org.apache.commons.logging.Log;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RelocationUtils {

    private static final Log log = CtpLogFactory.getLog(RelocationUtils.class);
    private static final String relocationCode = AppContext.getSystemProperty("relocationData.relocationCode");
    private static final FormApi4Cap4 cap4FormManager = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
    private static final String scheduleArchiveCode = AppContext.getSystemProperty("ebsRc.scheduleArchiveCode");
    private static final EnumManager enumManager = (EnumManager) AppContext.getBean("enumManagerNew");
    private static OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");

    public static JSONObject delRelocationData(JSONObject param) throws BusinessException {
        RelocationDao relocationDao = (RelocationDao) AppContext.getBean("relocationDao");
        FormBean scheduleArchiveForm = cap4FormManager.getFormByFormCode(scheduleArchiveCode);
        FormFieldBean codeBean = scheduleArchiveForm.getFieldBeanByDisplay("编号");
        Map<String,Object> delFormMap = new HashMap<>();
        delFormMap.put("tableName", codeBean.getOwnerTableName());
        delFormMap.put("noFie", codeBean.getName());
        delFormMap.put("no", param.getString("no"));
        relocationDao.delDqDbFromDateByNo(delFormMap);
        log.info("RelocationUtils.delRelocationData");
        return null;
    }

    public static JSONObject addNewRelocationData(JSONObject param) throws BusinessException {
        FormBean scheduleArchiveForm = cap4FormManager.getFormByFormCode(scheduleArchiveCode);
        FormFieldBean nameBean = scheduleArchiveForm.getFieldBeanByDisplay("姓名");
        FormFieldBean typeBean = scheduleArchiveForm.getFieldBeanByDisplay("类型");
        FormFieldBean startDateBean = scheduleArchiveForm.getFieldBeanByDisplay("开始日期");
        FormFieldBean endDateBean = scheduleArchiveForm.getFieldBeanByDisplay("结束日期");
        FormFieldBean codeBean = scheduleArchiveForm.getFieldBeanByDisplay("编号");
        FormFieldBean createDateBean = scheduleArchiveForm.getFieldBeanByDisplay("发起时间");
        FormFieldBean biaoshiBean = scheduleArchiveForm.getFieldBeanByDisplay("唯一标识");
        FormFieldBean chlxBean = scheduleArchiveForm.getFieldBeanByDisplay("出差类型");
        String wybs = param.getString("wybs");
        String type = param.getString("type");
        String startDate = param.getString("startDate");
        String endDate = param.getString("endDate");
        String codeValue = param.getString("codeValue");
        String createDate = param.getString("createDate");
        Long memberId = param.getLong("memberId");
        String trip = param.getString("trip");

        long enumId = typeBean.getEnumId();
        CtpEnumBean enumBean = enumManager.getEnum(enumId);
        List<CtpEnumItem> items = enumBean.getItems();
        long enumItemId = 0;
        for (CtpEnumItem item:items){
            String showvalue = item.getShowvalue();
            if (type.equals(showvalue)){
                enumItemId = item.getId();
                break;
            }
        }
        V3xOrgMember member = orgManager.getMemberById(memberId);
        JSONObject data = new JSONObject();
        JSONObject mainData = new JSONObject();
        mainData.put("uniqueValue", wybs);
        mainData.put("uniqueKey", biaoshiBean.getDisplay());
        mainData.put("templateCode",scheduleArchiveCode);
        mainData.put("isCap4", true);
        mainData.put("loginName", member.getLoginName());
        //主表信息
        JSONArray mainBodyArray = new JSONArray();
        JSONObject mainBody1 = new JSONObject();
        mainBody1.put("name", nameBean.getDisplay());
        mainBody1.put("value", memberId);
        mainBodyArray.add(mainBody1);
        JSONObject mainBody2 = new JSONObject();
        mainBody2.put("name", typeBean.getDisplay());
        mainBody2.put("value", enumItemId);
        mainBodyArray.add(mainBody2);
        JSONObject mainBody3 = new JSONObject();
        mainBody3.put("name", startDateBean.getDisplay());
        mainBody3.put("value", startDate);
        mainBodyArray.add(mainBody3);
        JSONObject mainBody4 = new JSONObject();
        mainBody4.put("name", endDateBean.getDisplay());
        mainBody4.put("value", endDate);
        mainBodyArray.add(mainBody4);
        JSONObject createJson = new JSONObject();
        createJson.put("name", createDateBean.getDisplay());
        createJson.put("value", createDate);
        mainBodyArray.add(createJson);
        JSONObject mainBody5 = new JSONObject();
        mainBody5.put("name", codeBean.getDisplay());
        mainBody5.put("value", codeValue);
        mainBodyArray.add(mainBody5);
        if (trip != null){
            JSONObject tripJson = new JSONObject();
            tripJson.put("name", chlxBean.getDisplay());
            tripJson.put("value", trip);
            mainBodyArray.add(tripJson);
        }
        data.put("mainData", mainBodyArray);
        mainData.put("data", data);
        return  mainData;

    }
}
